package com.tencent.bugly.beta.download;

import android.os.Build;
import android.util.Log;
import com.tencent.bugly.beta.global.ResBean;
import com.tencent.bugly.beta.global.d;
import com.tencent.bugly.beta.global.f;
import com.tencent.bugly.beta.tinker.TinkerManager;
import com.tencent.bugly.beta.ui.h;
import com.tencent.bugly.beta.upgrade.BetaGrayStrategy;
import com.tencent.bugly.beta.utils.e;
import com.tencent.bugly.proguard.an;
import com.tencent.bugly.proguard.p;
import com.tencent.bugly.proguard.q;
import com.tencent.bugly.proguard.w;
import java.io.File;
import java.util.Map;

/* compiled from: BUGLY */
/* loaded from: classes.dex */
public class a implements DownloadListener {
    final int a;
    final Object[] b;

    public a(int i, Object... objArr) {
        this.a = i;
        this.b = objArr;
    }

    @Override // com.tencent.bugly.beta.download.DownloadListener
    public void onReceive(DownloadTask downloadTask) {
        if (this.a != 2) {
            return;
        }
        ((h) this.b[0]).a(downloadTask);
    }

    @Override // com.tencent.bugly.beta.download.DownloadListener
    public void onCompleted(DownloadTask downloadTask) {
        try {
            int i = this.a;
            if (i == 1) {
                synchronized (this.b[0]) {
                    Map map = (Map) this.b[1];
                    if (map.size() == 0) {
                        return;
                    }
                    int i2 = 0;
                    for (DownloadTask downloadTask2 : map.values()) {
                        if (downloadTask2.getStatus() == 1) {
                            i2++;
                        }
                    }
                    an.c(i2 + " has completed", new Object[0]);
                    if (i2 < map.size()) {
                        return;
                    }
                    for (String str : map.keySet()) {
                        if (((DownloadTask) map.get(str)).getSaveFile() != null && ((DownloadTask) map.get(str)).getSaveFile().exists()) {
                            ResBean.a.a(str, ((DownloadTask) map.get(str)).getSaveFile().getAbsolutePath());
                        }
                    }
                    com.tencent.bugly.beta.global.a.a("rb.bch", ResBean.a);
                    f fVar = (f) this.b[0];
                    fVar.a();
                    fVar.b();
                }
            } else if (i == 2) {
                ((h) this.b[0]).a(downloadTask);
            } else if (i == 3) {
                com.tencent.bugly.beta.upgrade.c cVar = (com.tencent.bugly.beta.upgrade.c) this.b[0];
                BetaGrayStrategy betaGrayStrategy = cVar.b;
                if (betaGrayStrategy != null && betaGrayStrategy.a != null) {
                    if (cVar.f != null) {
                        e.a(new d(18, cVar.f, 3, Boolean.valueOf(cVar.g)));
                    }
                    an.a("apk download completed", new Object[0]);
                    p.a.a(new w("download", System.currentTimeMillis(), (byte) 0, downloadTask.getCostTime(), betaGrayStrategy.a.e, betaGrayStrategy.a.m, betaGrayStrategy.a.p, null));
                    if (com.tencent.bugly.beta.global.e.E.ad) {
                        File saveFile = downloadTask.getSaveFile();
                        Integer num = (Integer) this.b[1];
                        if (com.tencent.bugly.beta.global.a.a(com.tencent.bugly.beta.global.e.E.s, saveFile, betaGrayStrategy.a.f.a)) {
                            this.b[1] = 0;
                            p.a.a(new w("install", System.currentTimeMillis(), (byte) 0, 0L, betaGrayStrategy.a.e, betaGrayStrategy.a.m, betaGrayStrategy.a.p, null));
                        } else if (num.intValue() < 2) {
                            if (Build.VERSION.SDK_INT >= 24) {
                                this.b[1] = 0;
                                onFailed(downloadTask, 2080, "安装失败，请检查您的App是否兼容7.0设备");
                                downloadTask.delete(true);
                                return;
                            }
                            this.b[1] = Integer.valueOf(num.intValue() + 1);
                            downloadTask.delete(true);
                            cVar.c = com.tencent.bugly.beta.global.e.E.p.a(betaGrayStrategy.a.f.b, com.tencent.bugly.beta.global.e.E.t.getAbsolutePath(), null, betaGrayStrategy.a.f.b);
                            h.v.a(betaGrayStrategy.a, cVar.c);
                            cVar.c.addListener(this);
                            BetaReceiver.addTask(cVar.c);
                            cVar.c.download();
                        } else {
                            this.b[1] = 0;
                            onFailed(downloadTask, 2080, "file md5 verify fail");
                            downloadTask.delete(true);
                        }
                    }
                }
            } else if (i == 4) {
                q qVar = (q) this.b[0];
                Integer num2 = (Integer) this.b[1];
                BetaGrayStrategy betaGrayStrategy2 = qVar.b;
                if (betaGrayStrategy2 != null && betaGrayStrategy2.a != null) {
                    an.a("patch download success !!!", new Object[0]);
                    File saveFile2 = downloadTask.getSaveFile();
                    if (com.tencent.bugly.beta.global.a.a(saveFile2, betaGrayStrategy2.a.f.a, "SHA")) {
                        this.b[1] = 0;
                        if (com.tencent.bugly.beta.global.a.a(saveFile2, com.tencent.bugly.beta.global.e.E.H)) {
                            an.c("copy " + saveFile2.getAbsolutePath() + " to " + com.tencent.bugly.beta.global.e.E.H.getAbsolutePath() + " success!", new Object[0]);
                            if (qVar.c != null) {
                                an.c("delete temp file", new Object[0]);
                                qVar.c.delete(true);
                            }
                            com.tencent.bugly.beta.global.a.a("UPLOAD_PATCH_RESULT", false);
                            TinkerManager.getInstance().onDownloadSuccess(com.tencent.bugly.beta.global.e.E.H.getAbsolutePath(), com.tencent.bugly.beta.global.e.E.V);
                            return;
                        }
                        an.a("copy file failed", new Object[0]);
                        TinkerManager.getInstance().onDownloadFailure("copy file failure.");
                    } else if (num2.intValue() < 2) {
                        this.b[1] = Integer.valueOf(num2.intValue() + 1);
                        downloadTask.delete(true);
                        qVar.c = com.tencent.bugly.beta.global.e.E.p.a(betaGrayStrategy2.a.f.b, com.tencent.bugly.beta.global.e.E.t.getAbsolutePath(), null, betaGrayStrategy2.a.f.b);
                        qVar.c.setNeededNotify(false);
                        qVar.c.addListener(this);
                        BetaReceiver.addTask(qVar.c);
                        qVar.c.download();
                    } else {
                        this.b[1] = 0;
                        onFailed(downloadTask, 2080, "file sha1 verify fail");
                        downloadTask.delete(true);
                        TinkerManager.getInstance().onDownloadFailure("retry download patch too many times.");
                    }
                }
            }
        } catch (Exception e) {
            if (an.b(e)) {
                return;
            }
            e.printStackTrace();
        }
    }

    @Override // com.tencent.bugly.beta.download.DownloadListener
    public void onFailed(DownloadTask downloadTask, int i, String str) {
        try {
            int i2 = this.a;
            if (i2 == 1) {
                synchronized (this.b[0]) {
                    f fVar = (f) this.b[0];
                    fVar.a();
                    fVar.b();
                }
            } else if (i2 == 2) {
                ((h) this.b[0]).a(downloadTask);
            } else if (i2 == 3) {
                com.tencent.bugly.beta.upgrade.c cVar = (com.tencent.bugly.beta.upgrade.c) this.b[0];
                if (downloadTask != null) {
                    p.a.a(new w("download", System.currentTimeMillis(), (byte) 1, downloadTask.getCostTime(), cVar.b.a.e, cVar.b.a.m, cVar.b.a.p, null));
                }
                an.e("upgrade failed：(%d)%s", Integer.valueOf(i), str);
                Log.e(an.b, "download fail, please try later");
            } else if (i2 != 4) {
            } else {
                q qVar = (q) this.b[0];
                if (downloadTask != null) {
                    p.a.a(new w("download", System.currentTimeMillis(), (byte) 1, downloadTask.getCostTime(), qVar.b.a.e, qVar.b.a.m, qVar.b.a.p, null));
                }
                an.e("hotfix download failed：(%d)%s", Integer.valueOf(i), str);
                TinkerManager.getInstance().onDownloadFailure(str);
            }
        } catch (Exception e) {
            if (an.b(e)) {
                return;
            }
            e.printStackTrace();
        }
    }
}
